# Austerity and Aggression: Government Responses to IMF Conditionality
# Replication Code
# Comparative Manifesto Project (CMP) Data Robustness Checks 
# World Politics

# Packages
library(lmtest)
library(sandwich)
library(DataCombine)

# load dataset with variables from CMP and ParlGov datasets
load("cmp_regression_variables.RData")

# load dataset with IMF, MIDs, control vars 
prelimlagimp <- read.csv("prelimlagimp.csv")

# merge CMP and IMF/MID datasets to get CMP regression modeling data
full <- merge(cmp_regression_variables, prelimlagimp, by = c("ccode", "year"))

# Table A6: Cabinet Parties and External Relations Topics ####
modA6 <- lm(percent_external ~ count + gdppc + trade + population_total + cinc + major_power +
               polity2 + exec_yrs_in_office + ethnic_frac + absidealimportantdiff + 
               factor(ccode) + factor(year), data = subset(full, pgv_cabinet_party == 1))

# clustered standard errors
(seA6 <- coeftest(modA6, vcov = vcovHC(modA6, type = "HC0", cluster = full$ccode)))


# Table A7: Cabinet Parties and Economic Topics ####
modA7 <- lm(percent_econ ~ count + gdppc + trade + population_total + cinc + major_power +
              polity2 + exec_yrs_in_office + ethnic_frac + absidealimportantdiff +
              factor(ccode) + factor(year), data = subset(full, pgv_cabinet_party == 1))

# clustered standard errors
(seA7 <- coeftest(modA7, vcov = vcovHC(modA7, type = "HC0", cluster = full$ccode)))


# Table A8: Cabinet Parties and External Relations Topics, 1-Year Lag ####
# create lagged IMF count variable + lagged controls
full <- full[order(full$ccode,full$year),]
full <- slide(full, Var="count", GroupVar="ccode",
                    slideBy = -1, NewVar = "count_lag1")
full <- slide(full, Var="gdppc", GroupVar="ccode",
                slideBy = -1, NewVar = "gdppc_lag1")
full <- slide(full, Var="trade", GroupVar="ccode",
                slideBy = -1, NewVar = "trade_lag1")
full <- slide(full, Var="population_total", GroupVar="ccode",
                slideBy = -1, NewVar = "population_total_lag1")
full <- slide(full, Var="cinc", GroupVar="ccode",
                slideBy = -1, NewVar = "cinc_lag1")
full <- slide(full, Var="polity2", GroupVar="ccode",
                slideBy = -1, NewVar = "polity2_lag1")
full <- slide(full, Var="exec_yrs_in_office", GroupVar="ccode",
                slideBy = -1, NewVar = "exec_yrs_in_office_lag1")
full <- slide(full, Var="ethnic_frac", GroupVar="ccode",
                slideBy = -1, NewVar = "ethnic_frac_lag1")
full <- slide(full, Var="absidealimportantdiff", GroupVar="ccode",
              slideBy = -1, NewVar = "absidealimportantdiff_lag1")

modA8 <- lm(percent_external ~ count_lag1 + gdppc_lag1 + trade_lag1 + population_total_lag1 + 
                   cinc_lag1 + major_power + polity2_lag1 + exec_yrs_in_office_lag1 + 
                   ethnic_frac_lag1 + absidealimportantdiff_lag1 + factor(ccode) + factor(year), 
                  data = subset(full, pgv_cabinet_party == 1))

# clustered standard errors
(seA8 <- coeftest(modA8, vcov = vcovHC(modA8, type = "HC0", cluster = full$ccode)))


# Table A9: Cabinet Parties and Economic Topics, 1-Year Lag ####
modA9 <- lm(percent_econ ~ count_lag1 + gdppc_lag1 + trade_lag1 + population_total_lag1 + 
                   cinc_lag1 + major_power + polity2_lag1 + exec_yrs_in_office_lag1 + 
                   ethnic_frac_lag1 + absidealimportantdiff_lag1 + factor(ccode) + factor(year), 
                 data = subset(full, pgv_cabinet_party == 1))

# clustered standard errors
(seA9 <- coeftest(modA9, vcov = vcovHC(modA9, type = "HC0", cluster = full$ccode)))


